/**
 * @fileoverview Logging methods for the opauqe glass pane that covers the stage.
 */

/**
 * Types of events logged.
 */
var LogEvents = {
  QA_MOUSE: 'qa-mouse', // Mouse events on the QA page.
  PAGE: 'page',         // Page loads.
  CHART: 'chart',       // Chart interaction (select, mouseover).
  GRAPHIC: 'graphic'    // INT_GRAPHIC interaction (mouseover, mouseout).
}

/**
 * Initializes the track / trace logger.
 */
function initLogger() {
  track.event = trace.event;
  trace.debug(false);
  logs = '';
}

/**
 * Creates the event listeners for the glass pane.
 *
 * @param {string} id Of the user.
 * @parma {Element} div The glass pane element.
 */
function createMouseLogger(div) {
  div.onclick = logMouseEvent;
  div.ondbclick = logMouseEvent;
  div.onmousedown = logMouseEvent;
  div.onmousemove = logMouseEvent;
  div.onmouseup = logMouseEvent;
}

/**
 * Logs the event and move onto the last page if needed.
 *
 * @param {number} storyIndex Of the data story.
 * @param {number} conditionIndex Of the test condition.
 * @param {Event} evt Mouse event.
 * @param {?string} opt_evtType Actual event type to be logged, if provided.
 */
function logMouseEvent(evt) {
  evt = evt || window.event;
  var x = evt.offsetX ? evt.offsetX : evt.layerX;
  var y = evt.offsetY ? evt.offsetY : evt.layerY;

  var jsonLog = {
    assignId: assignId,
    storyIndex: storyIndex,
    conditionIndex: conditionIndex,
    lastSelectedRow: lastSelectedRow,
    timestamp: evt.timeStamp,
    x: x,
    y: y,
    type: evt.type,
    useragent: navigator.userAgent
  };

  logEvent(LogEvents.QA_MOUSE, jsonLog);

  if (jsonLog.type == 'click' || jsonLog.type == 'dbclick') {
    createSurveyPage();
  }
}

function logPageEvent(opt_page) {
  logEvent(LogEvents.PAGE, {page: opt_page || page});
}

function logChartEvent(type, entity) {
  logEvent(LogEvents.CHART, {type: type, entity: entity});
}

function logGraphicEvent(type, entity) {
  logEvent(LogEvents.GRAPHIC, {type: type, entity: entity});
}

function logEvent(eventType, jsonLog) {
  var date = new Date();
  var ts = date.getTime();
  jsonLog['assignId'] = assignId;
  jsonLog['ts'] =  ts;
  jsonLog['action'] = eventType;
  logs += JSON.stringify(jsonLog) + ',';
  /*trackEvent('vizattstudy', eventType, JSON.stringify(jsonLog),
     navigator.userAgent);  */
}

